﻿tileActor = gamvas.Actor.extend({
    create: function (name, x, y, char) {
        this._super(name, x, y, char);
        var st = gamvas.state.getCurrentState();
        this.setFile(st.resource.getImage('images/tile.png'));
        this.x = x;
        this.y = y;
        this.show = false;
        this.letter = char;
        this.visible = true;
    },

    draw: function (t) {
        if (this.visible)
            this._super(t);
    }
});

wheelActor = gamvas.Actor.extend({
    create: function (name, x, y) {
        this._super(name, x, y);
        var st = gamvas.state.getCurrentState();
        this.setFile(st.resource.getImage('images/wheel.png'));
        this.setCenter(128, 128);
        this.rotVelocity = 0;
        this.friction = 0.98;
        this.visible = false;
    },

    draw: function (t) {
        if (this.visible)
            this._super(t);
        this.rotVelocity = this.rotVelocity * this.friction;
        var rotSpeed = this.rotVelocity * t;
        if (rotSpeed < 0.001)
            this.rotVelocity = 0;
        this.rotate(rotSpeed);
    }
});

// define our state class by extending gamvas.State
gameState = gamvas.State.extend({
    init: function () {
        this.dim = gamvas.getCanvasDimension();
        this.alphabets = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
        this.word = 'THIS GAME ROCKS';
        // display the game board
        this.leftOffSetBoard = 50;
        this.yPosBoard = -150;
        this.lowerLimit = Math.floor(this.dim.w / 2);
        this.tiles = [];
        this.leftOffSetKeyboard = 100;
        this.yPosKeyboard = 0;
        this.keyboard = [];
        this.fireworks = [];

        for (var i = 0; i < this.word.length; i++) {
            if (this.word.charAt(i) != ' ') {
                var newTile = new tileActor('tile' + i, this.leftOffSetBoard + (32 * i) - this.lowerLimit, this.yPosBoard, this.word.charAt(i));
                this.tiles.push(newTile);
                this.addActor(newTile);
            }
        }

        var st = gamvas.state.getCurrentState();
        var xPos = 0;
        for (var j = 0; j < this.alphabets.length; j++) {
            var newKey = new tileActor('key' + j, this.leftOffSetKeyboard + (32 * xPos) - this.lowerLimit, this.yPosKeyboard, this.alphabets.charAt(j));
            newKey.setFile(st.resource.getImage('images/' + newKey.letter + '.png'));
            this.keyboard.push(newKey);
            this.addActor(newKey);
            xPos++;
            if (j != 0 && j % 15 == 0) {
                this.yPosKeyboard += 50;
                xPos = 0;
            }
        }

        this.wheel = new wheelActor('wheel', 0, 250);
        this.addActor(this.wheel);
        this.state = 'board';
    },

    postDraw: function (t) {
        if (checkGameOver(this.tiles)) {
            for (var i = this.tiles.length; i >= 0; i--) {
                this.removeActor(this.tiles[i]);
                delete this.tiles[i];
            }
            //gamvas.state.setState('menu');
        }
    },

    draw: function (t) {
        if (this.state == 'wheel') {
            for (var i = 0; i < this.keyboard.length; i++) {
                this.keyboard[i].visible = false;
            }
            this.wheel.visible = true;
        }
        else if (this.state == 'board') {
            for (var i = 0; i < this.keyboard.length; i++) {
                this.keyboard[i].visible = false;
            }
            this.wheel.visible = false;
        }
        else if (this.state == 'keyboard') {
            for (var i = 0; i < this.keyboard.length; i++) {
                this.keyboard[i].visible = true;
            }
            this.wheel.visible = true;
        }
    },

    onKeyDown: function (k) {
        if (this.state == 'board') {
            if (k == gamvas.key.SPACE) {
                var rotSpeed = 10 + (Math.random() % 10);
                this.wheel.rotVelocity = rotSpeed;
                this.state = 'wheel';
            }
        }
        else if (this.state == 'wheel') {
            if (this.wheel.rotVelocity == 0) {
                this.state = 'keyboard';
            }
        }
        else if (this.state == 'keyboard') {
            if (k >= gamvas.key.A && k <= gamvas.key.Z) {
                var st = gamvas.state.getCurrentState();
                for (var i = 0; i < this.tiles.length; i++) {
                    if (k == this.tiles[i].letter.charCodeAt(0)) {
                        this.tiles[i].setFile(st.resource.getImage('images/' + this.tiles[i].letter + '.png'));
                        this.tiles[i].show = true;

                        this.emitter = new fireworkEmitter('boom', this.tiles[i].x, this.tiles[i].y);
                        this.addActor(this.emitter);
                    }
                }
            }
            this.state = 'board';
        }
        return false;
    },

    onMouseDown: function (b, x, y) {
        if (b == gamvas.mouse.LEFT) {
            // get our world coordinates of the mouse screen coordinates
            var wp = this.camera.toWorld(x, y);
            // update our emitter position
            this.emitter.setPosition(wp.x, wp.y);
            // update restart (whith old particles continuing to live)
            // if your want to force a reset, so old particles are
            // killed immediately, use reset(true)
            this.emitter.reset();
        }
    }
});

function checkGameOver(tiles) {
    // check if game over
    var isGameOver = true;
    for (var i = 0; i < tiles.length; i++) {
        if (!tiles[i].show) {
            isGameOver = false;
            break;
        }
    }
    return isGameOver;
}
